package com.genius.utils;

import com.genius.enums.ResponseType;
import com.genius.exception.GraceException;
import org.springframework.core.io.ClassPathResource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.MessageFormat;

/**
 * 文件处理工具类
 */
public class FileUtils {

    public static String  buildContent(String fileName, Object ... arguments) {
        StringBuilder builder = new StringBuilder();
        try {
            builder.append(buildHtmlContent(fileName));
        } catch (IOException e) {
            GraceException.display(ResponseType.BUILD_EMAIL_CONTENT_ERROR);
        }
        return MessageFormat.format(builder.toString(), arguments);
    }

    public static StringBuilder buildHtmlContent(String fileName) throws IOException {
        ClassPathResource classPathResource = new ClassPathResource(fileName);
        InputStream inputStream = classPathResource.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder builder = new StringBuilder();
        String line = "";
        try {
            while ((line = br.readLine()) != null) {
                builder.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            inputStream.close();
            br.close();
        }
        return builder;
    }
}